
namespace Behemoth.SharePoint.Utilities {

    using System;
    using System.Runtime.InteropServices;
    using System.IO;
    using System.Collections.Generic;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Xml;
    using System.Xml.XPath;
    using System.Xml.Serialization;
    using System.Security;
    using System.Security.Permissions;
    using System.Globalization;

    using Microsoft.Win32;
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Administration;
    using Microsoft.SharePoint.WebPartPages;
    using Microsoft.SharePoint.Deployment;
    using Microsoft.SharePoint.Security;

    // TODO dredge this class for anything useful and then get rid of it
	public class ProvisioningTools {

		/// <summary>
		/// 
		/// </summary>
		/// <param name="fieldName"></param>
		/// <param name="group"></param>
		/// <param name="required"></param>
		/// <param name="allowMultipleValues"></param>
		/// <param name="w"></param>
		/// <param name="lookupList"></param>
		/// <param name="lookupField"></param>
		/// <remarks>
		/// This method provided by http://www.sharepointblogs.com/tmt/archive/2007/09/06/sharepoint-programmatically-provisioning-lookup-fields.aspx
		/// SPFieldLookup lookup = CreateLookupField("CustomLookup", "Custom fields", false, false, SPContext.Current.Site.RootWeb, SPContext.Current.Site.AllWebs["MyWeb"].Lists["CustomList"], "Title");
		/// </remarks>
		/// <returns></returns>
		public static SPFieldLookup CreateLookupField( string fieldName, string group, bool required, bool allowMultipleValues, SPWeb w, SPList lookupList, string lookupField ) {
			w.Fields.AddLookup( fieldName, lookupList.ID, lookupList.ParentWeb.ID, required );
			SPFieldLookup lookup = (SPFieldLookup)w.Fields[fieldName];
			lookup.AllowMultipleValues = allowMultipleValues;
			lookup.LookupField = lookupField;
			lookup.Group = group;
			lookup.Update( true );
			return lookup;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="web"></param>
		/// <param name="contentType"></param>
		/// <param name="fieldName"></param>
		/// <remarks>
		/// This method provided by http://www.sharepointblogs.com/tmt/archive/2007/09/06/sharepoint-programmatically-provisioning-lookup-fields.aspx
		/// </remarks>
		public static void LinkFieldToContentType( SPWeb web, string contentType, SPField field ) {
			SPContentType ct = web.ContentTypes[contentType];
			ct.FieldLinks.Add( new SPFieldLink( field ) );
			ct.Update();
		}

		#region Cleanup for CustomFieldTypes

		// TODO we should really move these into the custom SPField classes themselves
		// but that would create a dependency I don't wnat to worry about right now...

        [Obsolete("Please use SPFieldExtensions' SPField.SetDisplayOnlyFieldProperties instead.")]
		public static void SetupDisplayOnlyFieldProperties( SPField field, string newFormDisplayMode, string editFormDisplayMode ) {
            field.SetDisplayOnlyFieldProperties(newFormDisplayMode, editFormDisplayMode, true);
		}

		public static void SetupBannerFieldProperties( SPField field ) {
			SetupBannerFieldProperties( field, true, true, true );
		}
		public static void SetupBannerFieldProperties( SPField field, bool showInViewForm, bool showInNewForm, bool showInEditForm ) {
			field.SetCustomProperty( "ImageUrl", "" );
			field.SetCustomProperty( "ShowInViewForm", showInViewForm ? "Yes" : "No");
			field.SetCustomProperty( "ShowInNewForm", showInNewForm ? "Yes" : "No" );
			field.SetCustomProperty( "ShowInEditForm", showInEditForm ? "Yes" : "No" );
			field.Update();
		}

		// TODO make this much more customizable later on
		public static void SetupDropDownFieldProperties( SPField field, string dspName, string listUrl ) {
			field.SetCustomProperty( "XmlPath", dspName );
			field.SetCustomProperty( "DataValueField", "ID" );
			field.SetCustomProperty( "DataTextField", "Title" );
			field.SetCustomProperty( "DataTextFormatString", "" );
			field.SetCustomProperty( "ToolTip", "" );
			field.SetCustomProperty( "RenderControlType", "Drop Down List: a simple drop down list that allows selection of a single item" );
			field.SetCustomProperty( "BlankItemDisplayMode", "Always" );
			field.SetCustomProperty( "BlankItemValue", "" );
			field.SetCustomProperty( "BlankItemTextRequired", "select..." );
			field.SetCustomProperty( "BlankItemTextOptional", "(none)" );
			field.SetCustomProperty( "UrlTemplateView", listUrl + "/DispForm.aspx?ID=[Value]" );
			field.SetCustomProperty( "UrlTemplateEdit", listUrl + "/EditForm.aspx?ID=[Value]" );
			field.Update();
		}

		#endregion

	} // class

} // namespace
